import {ref} from "vue";
import useZPaging from "z-paging/components/z-paging/js/hooks/useZPaging";
import {onLoad, onPageScroll, onReachBottom} from "@dcloudio/uni-app";
import {getMemberInAPI, getSignInAPI} from "../../services/me";

export const useGoldDetail = () => {
    const paging = ref(null)
    let dataList = ref([])
    useZPaging(paging)

    const my_gold_coin = ref(0)
    // 菜单下标
    const tabIndex = ref(0)

    onLoad((options) => {
        if (options.my_gold_coin) {
            my_gold_coin.value = options.my_gold_coin
        }
    })
    const okBack = () => {
        uni.navigateBack({})
    }
    // 获取金币明细列表
    const handleSignInLists = (pageNo, pageSize) => {
        getSignInAPI(pageNo, pageSize).then(res => {
            setTimeout(() => {
                paging.value.complete(res.result);
            }, 600)
        }).catch(() => {
            paging.value.complete(false);
        })
    }
    // 获取兑换列表
    const handleMemberLists = (pageNo, pageSize) => {
        getMemberInAPI({
            limit: pageNo,
            page: pageSize
        }).then(res => {
            setTimeout(() => {
                paging.value.complete(res.result);
            }, 600)
        }).catch(() => {
            paging.value.complete(false);
        })
    }
    // 获取金币数据
    const queryList = (pageNo, pageSize) => {
        if (tabIndex.value === 0) {
            handleSignInLists(pageNo, pageSize)
        } else {
            handleMemberLists(pageNo, pageSize)
        }
    }
    // 菜单切换
    const onTabChange = (index) => {
        tabIndex.value = index
        paging.value.reload();
    }
    onPageScroll(e => {
    })
    onReachBottom(() => {
    })
    return {
        paging,
        dataList,
        my_gold_coin,
        tabIndex,
        okBack,
        handleSignInLists,
        handleMemberLists,
        queryList,
        onTabChange
    }
}